

//生成评论的
import articleApi from "@/apis/articleApi"
import { User } from "@/apis/userApi"
import utils from "@/utils"
import { ElMessage } from "element-plus"
import { defineStore} from "pinia"
import userStore from "./userStore"


export default defineStore('comment',{

    state:()=>{
        return {
             commentInfo:[] as string[] ,
             userInfo:{}   as  null |User,
             commitTime:-20 as number
        }
    },
    actions:{ 
       

        //获取文章的评论(这里先存到本地，后面存到数据库)
        async getComments(b_id:any){
         this.commentInfo= (await articleApi.getCommentsInfoByBid(b_id)).data as []
                   //发送请求获取mysql的评论    
                   return this.commentInfo   
         },
         async SetComments(b_id:any,u_id:any,comments:any){
               (await articleApi.setComment(b_id,u_id,comments) ).data 
               //
             await  this.getComments(b_id)
        }
        ,
        //获取发表评论的人
        getUserByComment(){
            //通过登录返回的用户信息获取当前是那个用户
           this.userInfo=  userStore().info
        },

        setCommentTime(time:number){
            //毫秒的形式
            this.commitTime =time
        },
        getCurrentTime(){
            return new Date().getTime()
        },

        //删除评论
       async deleteComment(c_id:any,b_id:any){


        const res=  (await articleApi.deleteComment(c_id,b_id))  as {msg:string,code:number}
       
        await this.getComments(b_id)
       

         if(res?.msg=="删除成功"){

       
         ElMessage({
             message: '删除成功',
             type: 'success',
         })
         }
 
            else{
                ElMessage({
                    message: '删除失败',
                    type: 'warning',
                }) 
            }
        }
        
    
       }
    }
)